<script lang="jsx">
import { type, getBtnDefaultSetting, buttonKeys } from './utils'

export default {
  name: 'FuncBtn',
  functional: true,
  render(h, { props, listeners }) {
    const { config, func, rowProps, ...otherProps } = props

    if (!config) return null

    const { defaultProps, defaultText } = getBtnDefaultSetting(func)

    if (type(config) === 'string') {
      return (
        <ElButton
          {
            ...{
              props: { ...defaultProps, ...otherProps },
              on: listeners
            }
          }
        >
          { defaultText }
        </ElButton>
      )
    }

    // 当配置是一个对象时
    const { text, disabled = () => false, attrs } = config
    const isDisabled = func === buttonKeys.EDIT || func === buttonKeys.DEL ? disabled(rowProps) : false

    return (
      <ElButton
        {
          ...{
            props: {
              disabled: isDisabled,
              ...defaultProps,
              ...attrs,
              ...otherProps
            },
            on: listeners
          }
        }
      >
        { text || defaultText}
      </ElButton>
    )
  }
}
</script>